//
//  TodayStatisticsController.m
//  Water
//
//  Created by yh on 2017/12/1.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "TodayStatisticsController.h"
#import "AppDataBaseManager.h"
#import <NSDate+GBExt.h>
#import <YYKit/NSDate+YYAdd.h>
#import <Masonry/Masonry.h>
@interface TodayStatisticsController ()

@end

@implementation TodayStatisticsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.todayStatisticsView];
    
    
    if (self.userId) {
        RecordsParam *param = [[RecordsParam alloc]init];
        param.userid = self.userId;
        [WaterAPI recordsWith:param progress:nil success:^(ApiResponse *response) {
            
            NSMutableArray *categoriesSet = [[NSMutableArray alloc]init];
            NSMutableArray *seriesSet = [[NSMutableArray alloc]init];
            for (NSDictionary *item in response.data[@"waterRecords"]) {
                DBRecord *record = [[DBRecord alloc]init];
                record.time = item[@"time"];
                record.amount = [item[@"amount"] intValue];
                record.time = [NSDate gb_timeStringOf:record.time format:@"yyyy-MM-dd HH:mm:ss"];
                [categoriesSet addObject:[[NSDate dateWithString:record.time format:@"yyyy-MM-dd HH:mm:ss"] stringWithFormat:@"HH:mm:ss"]];
                [seriesSet addObject:@(record.amount)];
            }
            AAChartModel *chartModel= AAObject(AAChartModel)
            .chartTypeSet(AAChartTypeBar)//设置图表的类型(这里以设置的为柱状图为例)
            .titleSet(@"今日饮水记录")//设置图表标题
            .subtitleSet(@"时间")//设置图表副标题
            .categoriesSet(categoriesSet)//设置图表横轴的内容
            .yAxisTitleSet(@"毫升")//设置图表 y 轴的单位
            
            .seriesSet(@[
                         AAObject(AASeriesElement)
                         .nameSet(@"饮水")
                         .dataSet(seriesSet),
                         ]);
            /*图表视图对象调用图表模型对象,绘制最终图形*/
            [self.todayStatisticsView.chartView aa_drawChartWithChartModel:chartModel];
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
    }else{
        [[AppDataBaseManager shareManager] getTodayRecordscallBack:^(NSArray *records) {
            NSMutableArray *categoriesSet = [[NSMutableArray alloc]init];
            NSMutableArray *seriesSet = [[NSMutableArray alloc]init];
            for (DBRecord *item in records) {
                [categoriesSet addObject:[[NSDate dateWithString:item.time format:@"yyyy-MM-dd HH:mm:ss"] stringWithFormat:@"HH:mm:ss"]];
                [seriesSet addObject:@(item.amount)];
            }
            AAChartModel *chartModel= AAObject(AAChartModel)
            .chartTypeSet(AAChartTypeBar)//设置图表的类型(这里以设置的为柱状图为例)
            .titleSet(@"今日饮水记录")//设置图表标题
            .subtitleSet(@"时间")//设置图表副标题
            .categoriesSet(categoriesSet)//设置图表横轴的内容
            .yAxisTitleSet(@"毫升")//设置图表 y 轴的单位
            
            .seriesSet(@[
                         AAObject(AASeriesElement)
                         .nameSet(@"饮水")
                         .dataSet(seriesSet),
                         ]);
            /*图表视图对象调用图表模型对象,绘制最终图形*/
            [self.todayStatisticsView.chartView aa_drawChartWithChartModel:chartModel];
        }];
    }
    

    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (TodayStatisticsView *)todayStatisticsView{
    if (!_todayStatisticsView) {
        _todayStatisticsView  = [[TodayStatisticsView alloc]init];
        [self.view addSubview:_todayStatisticsView];
        [_todayStatisticsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    return _todayStatisticsView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
